﻿using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using CurriculumData;
using DevExpress.Web.ASPxEditors;

namespace CurriculumSite
{
    public partial class Profile : Page
    {
        #region Members

        private readonly Controller _controller = new Controller();
        
        #endregion

        #region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CandidateId"] == null)
                Response.Redirect("~/Entry.aspx");
            EnableAttachment();
            if (IsPostBack)
                return;
            var stateASPxComboBox = (ASPxComboBox)FormView.Row.FindControl("stateASPxComboBox");
            foreach (var state in _controller.States)
                stateASPxComboBox.Items.Add(state[0].ToString(), state[0]);
        }

        protected void LinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            e.Result = _controller.GetCandidate(Convert.ToInt32(Session["CandidateId"]));
        }

        protected void FormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            var candidate = _controller.GetCandidate(Convert.ToInt32(Session["CandidateId"]));
            _controller.Transfer(candidate, e.NewValues);
            SetPhoto(candidate);
            SetAttachment(candidate);
            _controller.Save(candidate);
            EnableAttachment();
            e.Cancel = true;
        }

        #endregion

        #region Private Methods

        private void EnableAttachment()
        {
            var candidate = _controller.GetCandidate(Convert.ToInt32(Session["CandidateId"]));
            var attachmentASPxHyperLink = (ASPxHyperLink)FormView.Row.FindControl("attachmentASPxHyperLink");
            attachmentASPxHyperLink.Enabled = candidate.Attachment != null;
        }

        private void SetAttachment(Candidate candidate)
        {
            var attachmentFileUpload = (FileUpload)FormView.Row.FindControl("attachmentFileUpload");
            if (!attachmentFileUpload.HasFile)
                return;
            candidate.Attachment = attachmentFileUpload.FileBytes;
            candidate.AttachmentType = attachmentFileUpload.PostedFile.ContentType;            
            candidate.AttachmentName = Path.GetFileName(attachmentFileUpload.PostedFile.FileName);      
        }

        private void SetPhoto(Candidate candidate)
        {
            var imageFileUpload = (FileUpload)FormView.Row.FindControl("imageFileUpload");
            if (!imageFileUpload.HasFile)
                return;
            candidate.Photo = imageFileUpload.FileBytes;
            candidate.PhotoType = imageFileUpload.PostedFile.ContentType;
        }

        #endregion

        #region Protected Overrides

        protected void stateASPxComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var stateASPxComboBox = (ASPxComboBox)FormView.Row.FindControl("stateASPxComboBox");
            var provinceASPxComboBox = (ASPxComboBox)FormView.Row.FindControl("provinceASPxComboBox");
            var cityASPxComboBox = (ASPxComboBox)FormView.Row.FindControl("cityASPxComboBox");
            provinceASPxComboBox.Items.Clear();
            provinceASPxComboBox.Text = "";
            cityASPxComboBox.Items.Clear();
            cityASPxComboBox.Text = "";
            var list = _controller.GetProvincies(stateASPxComboBox.Text);
            foreach (var province in list)
                provinceASPxComboBox.Items.Add(province[0].ToString(), province[0]);
        }

        protected void ProvinceAsPxComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            var stateASPxComboBox = (ASPxComboBox)FormView.Row.FindControl("stateASPxComboBox");
            var provinceASPxComboBox = (ASPxComboBox)FormView.Row.FindControl("provinceASPxComboBox");
            var cityASPxComboBox = (ASPxComboBox)FormView.Row.FindControl("cityASPxComboBox");
            cityASPxComboBox.Items.Clear();
            cityASPxComboBox.Text = "";
            var list = _controller.GetCities(stateASPxComboBox.Text, provinceASPxComboBox.Text);
            foreach (var city in list)
                cityASPxComboBox.Items.Add(city[0].ToString(), city[0]);
        }

        #endregion
    }
}
